<?php
/**
 * Created by PhpStorm.
 * User: Panjing
 * Date: 2021/1/3
 * Time: 10:55
 */
namespace app\publicuser;

use think\Config;
use think\Log;

class SignatureAndVerification
{
    /*
     * 加签名
     */
    public static function sign_with_sha1_with_rsa($contentForSign)
    {
        $data = base64_encode($contentForSign);
        $certs=array();
        $filePath = 'static/resources/certificate/66668888.pfx';
        $keyPass = Config::get('prikey');//'66668888';
        $pkcs12 = file_get_contents($filePath);
        if(openssl_pkcs12_read($pkcs12,$certs,$keyPass)){
            $privateKey = $certs['pkey'];
//            $publicKey =$certs['cert'];
            $signedMsg = '';
            if(openssl_sign($data,$signedMsg,$privateKey)){
                $signedMsg = base64_encode(($signedMsg));
                return $signedMsg;
            }
            return '';
        }
        return '';
    }

    /*
    * 加签名
    */
    public static function sign_with_sha1_with_rsa_pipixia($contentForSign)
    {
        $data = base64_encode($contentForSign);
        $certs=array();
        $filePath = 'static/resources/certificate/66668888pipixia.pfx';
        $keyPass = Config::get('prikey');//'66668888';
        $pkcs12 = file_get_contents($filePath);
        if(openssl_pkcs12_read($pkcs12,$certs,$keyPass)){
            $privateKey = $certs['pkey'];
//            $publicKey =$certs['cert'];
            $signedMsg = '';
            if(openssl_sign($data,$signedMsg,$privateKey)){
                $signedMsg = base64_encode(($signedMsg));
                return $signedMsg;
            }
            return '';
        }
        return '';
    }


    /*
     * 加签名
     */
    public static function sign_with_sha1_with_rsa_breakfast($contentForSign)
    {
        $data = base64_encode($contentForSign);
        $certs=array();
        $filePath = 'static/resources/certificate/266668888.pfx';
        $keyPass = Config::get('prikey');//'66668888';
        $pkcs12 = file_get_contents($filePath);
        if(openssl_pkcs12_read($pkcs12,$certs,$keyPass)){
            $privateKey = $certs['pkey'];
//            $publicKey =$certs['cert'];
            $signedMsg = '';
            if(openssl_sign($data,$signedMsg,$privateKey)){
                $signedMsg = base64_encode(($signedMsg));
                return $signedMsg;
            }
            return '';
        }
        return '';
    }

    /*
     * 验签
     * $contentBody 返回报文中加密json字符串
     * $signature 加签字符串
     * 返回值 1：验签成功
     */
    public static function  read_cer_and_verify_sign($contentBody, $signature)
    {
        $filePath = 'static/resources/certificate/TrustPay.cer';
        $certificateCAcerContent = file_get_contents($filePath);
        $certificateCApemContent = '-----BEGIN CERTIFICATE-----'.PHP_EOL
            .chunk_split(base64_encode($certificateCAcerContent),64,PHP_EOL)
            .'-----END CERTIFICATE-----'.PHP_EOL;
        $key = openssl_get_publickey($certificateCApemContent);

        $ok = (bool)openssl_verify($contentBody,base64_decode($signature),$key);
        openssl_free_key($key);
        return $ok;
    }


    /*
  * 加签名MD5
  */
    public static function TestTel($recharge_no)
    {
        $url='http://114.55.205.222:80/apply/hf?sign=';
//        $recharge_no='13814812547';
        $amount='1';
        $aaa= mt_rand(1,100);
         $user_order_no='hfTest00000000000'.$aaa;
        $sign=SignatureAndVerification::sign_with_Md5($recharge_no,$amount,$user_order_no);
        $headers = array();
        array_push($headers, 'Content-type:application/json;charset="utf-8"');

        $options = array(
            'http' => array(
                'method' => 'GET',
                'header' => $headers,
            )
        );
        $test="&user_order_no=hfTest0000000000001&uid=test&amount=0";
        $url=$url.$sign.'&recharge_no='.$recharge_no.'&user_order_no='.$user_order_no.'&uid=jianabc&amount='.$amount;

        Log::info($url);


        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);

        $content = stream_context_create($options);
        $result = null;
        Log::info($content);
        $result = file((string)$url, 1, $content);
        Log::info($result);
        $output = $result[0];
        $resultjson=json_decode($output);
        Log::info($resultjson);
    }



    /*
   * 加签名MD5 80充
   */
    public static function sign_with_Md5($recharge_no,$amount, $user_order_no)
    {
        $appkey='9eca45c4862011e6be0ac14a77748e68';
        $signstr=$recharge_no.$amount.$user_order_no.$appkey;
        Log::info($signstr);
        $data = md5($signstr);

        return $data;
    }

    /*
 * 加签名MD5天聚人合
 */
    public static function sign_tjwith_Md5($phoneno,$cardnum,$orderid)
    {
        //md5( OpenID+key+phoneno+cardnum+orderid)
        $appkey='5fa3415aad346d51510ee6853a02e1f7';
        $openid='JHbc5ca308d4c6886214db8afad4c28121';
        $signstr=$openid.$appkey.$phoneno.$cardnum.$orderid;
        Log::info('signstr:'.$signstr);
        $data = md5($signstr);
        Log::info('sign:'.$signstr);
        return $data;
    }
}